Skip to content

feat: update scheduled tasks to run in background#359

Merged
DaniAkash merged 4 commits intomainfrom
feat/scheduled-tasks-update
Feb 24, 2026
Merged

feat: update scheduled tasks to run in background#359
DaniAkash merged 4 commits intomainfrom
feat/scheduled-tasks-update

Conversation

@DaniAkash
Copy link
Contributor

This pull request introduces major improvements to how scheduled background tasks are handled by the agent. It ensures scheduled tasks run in a dedicated, hidden browser window, keeping them isolated from the user's visible browser context. The changes also update the system prompt and browser context formatting to support this new behavior, and add proper cleanup of hidden windows after task completion. Additionally, there are minor UI improvements for scheduled task result dialogs.

Scheduled Task Isolation and Lifecycle Management

  • Scheduled tasks now run in a dedicated hidden browser window, with a new windowId and isolated browser context; this window is created when a scheduled task session starts and is closed when the task completes or the session is deleted. [1] [2] [3]
  • The agent's session now tracks both the hiddenWindowId and the browser context for scheduled tasks, ensuring all agent operations use the correct context. [1] [2]

Prompt and Instruction Updates

  • The system prompt builder and strict rules now support conditional logic for scheduled tasks, injecting a <scheduled_task> section with critical rules (such as not grouping tabs and using the correct windowId), and disabling tab grouping in strict rules for scheduled tasks. [1] [2] [3] [4] [5]

Browser Context Formatting

  • The browser context formatter now displays the windowId and pageId for tabs, clarifying which window and page the agent is operating in—especially important for scheduled tasks.

UI Improvements for Scheduled Tasks

  • The scheduled tasks page now keeps the viewed run dialog in sync with storage updates, ensuring the latest status/result is shown while the dialog is open. [1] [2]

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 24, 2026

Greptile Summary

This PR successfully isolates scheduled background tasks into dedicated hidden browser windows, preventing interference with the user's visible browser sessions. The implementation includes:

  • Hidden Window Lifecycle: Scheduled tasks now create a hidden browser window at session start, which is automatically closed when the task completes or the session is deleted
  • Context Isolation: The agent receives a properly scoped browserContext with the hidden windowId and pageId, ensuring all operations stay within the isolated window
  • Prompt Customization: The system prompt conditionally injects scheduled task instructions that disable tab grouping and guide the agent to use the correct window/page IDs
  • UI State Management: The scheduled tasks page now derives the viewing run from storage, keeping the dialog in sync with live updates
  • Race Condition Prevention: Both onFinish and deleteSession handlers properly set hiddenWindowId = undefined before closing the window, preventing double-close attempts

The changes are well-structured, with proper separation of concerns and consistent error handling throughout.

Confidence Score: 5/5

  • This PR is safe to merge with no critical issues found
  • The implementation is well-designed with proper lifecycle management, error handling, and race condition prevention. The hidden window cleanup is handled in both completion and deletion paths, with proper guards against double-close. The prompt modifications correctly scope the agent's behavior for scheduled tasks, and the UI improvements enhance real-time visibility.
  • No files require special attention

Important Files Changed

Filename Overview
apps/agent/entrypoints/app/scheduled-tasks/ScheduledTasksPage.tsx Improved state management by deriving viewingRun from jobRuns storage, ensuring the dialog shows latest run status/result
apps/server/src/agent/prompt.ts Added conditional scheduled task instructions with windowId support and made strict rules exclude tab grouping for scheduled tasks
apps/server/src/agent/tool-loop/service.ts Implemented hidden window creation for scheduled tasks with proper cleanup in both onFinish and deleteSession handlers
apps/server/src/agent/tool-loop/session-store.ts Extended session interface to track hiddenWindowId and isolated browser context for scheduled tasks

Sequence Diagram

sequenceDiagram
    participant User
    participant ChatV2Service
    participant Browser
    participant AiSdkAgent
    participant Session
    
    User->>ChatV2Service: processMessage(isScheduledTask=true)
    
    alt New Session
        ChatV2Service->>Browser: createWindow({hidden: true})
        Browser-->>ChatV2Service: {windowId}
        ChatV2Service->>Browser: newPage('about:blank', {windowId})
        Browser-->>ChatV2Service: pageId
        ChatV2Service->>ChatV2Service: Create browserContext with windowId, pageId
        ChatV2Service->>AiSdkAgent: create(browserContext)
        AiSdkAgent->>AiSdkAgent: buildSystemPrompt(isScheduledTask, windowId)
        AiSdkAgent-->>ChatV2Service: agent
        ChatV2Service->>Session: store(agent, hiddenWindowId, browserContext)
    end
    
    ChatV2Service->>AiSdkAgent: appendUserMessage(messageContext)
    ChatV2Service->>AiSdkAgent: stream response
    AiSdkAgent-->>User: Execute task in hidden window
    
    AiSdkAgent->>ChatV2Service: onFinish callback
    ChatV2Service->>Session: Set hiddenWindowId = undefined
    ChatV2Service->>Browser: closeWindow(windowId)
    
    alt deleteSession called
        User->>ChatV2Service: deleteSession()
        ChatV2Service->>Session: Get hiddenWindowId
        ChatV2Service->>Session: Set hiddenWindowId = undefined
        ChatV2Service->>Browser: closeWindow(windowId)
        ChatV2Service->>Session: delete()
    end
Loading

Last reviewed commit: 7af229a

@DaniAkash
Copy link
Contributor Author

@greptileai

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@DaniAkash
Copy link
Contributor Author

@greptileai

@DaniAkash DaniAkash merged commit 1322638 into main Feb 24, 2026
7 of 9 checks passed
@DaniAkash DaniAkash deleted the feat/scheduled-tasks-update branch February 24, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant